package teleradiologia.repository;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.HibernateTemplate;

import teleradiologia.domain.Ordertype;
import teleradiologia.domain.Person;
import teleradiologia.utils.HibernateUtil;

public class OrdertypeDaoImpl implements OrdertypeDao {

	@Override
	public long create(Ordertype ordertype) {
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();
		
		long id = (Long)session.save(ordertype);
		
		tx.commit();
		session.close();
	
		return ++id;
	}

	@Override
	public void delete(Ordertype ordertype) {
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		session.delete(ordertype);

		tx.commit();
		session.close();
	}
	
	@Override
	public List<Ordertype> getAll() {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();
		
		List<Ordertype> ordertype = session.createQuery(
				"from Ordertype ordertype order by ordertype.id asc").list();
		
		tx.commit();
		session.close();

		return ordertype;
	}

	@Override
	public Ordertype getById(long id) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();
		
		Ordertype ordertype = (Ordertype) session.get(Ordertype.class, new Long(id));
		
		tx.commit();
		session.close();

		return ordertype;
	}

	@Override
	public void update(Ordertype ordertype) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		session.update(ordertype);

		tx.commit();
		session.close();
	}
}
